load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "lexbase",
    srcs = [
        "encode.go",
        "experimental_keywords.go",
        "normalize.go",
        "predicates.go",
        ":gen-keywords",  # keep
        ":gen-reserved-keywords",  # keep
        ":gen-tokens",  # keep
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/lexbase",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/util/stringencoding",
        "@org_golang_x_text//unicode/norm",
    ],
)

go_test(
    name = "lexbase_test",
    size = "small",
    srcs = ["encode_test.go"],
    deps = [
        ":lexbase",
        "//pkg/sql/parser",
    ],
)

# Define the target to auto-generate our list of keywords from the grammar file.
genrule(
    name = "gen-keywords",
    srcs = [
        "//pkg/sql/parser:sql.y",
    ],
    outs = ["keywords.go"],
    cmd = """
        $(location //pkg/sql/lexbase/allkeywords) < $(location //pkg/sql/parser:sql.y) > $@
    """,
    tools = [
        "//pkg/sql/lexbase/allkeywords",
    ],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

# Define the target to auto-generate a list of tokens from the grammar file.
genrule(
    name = "gen-tokens",
    outs = ["tokens.go"],
    cmd = """
    (echo "// Code generated by make. DO NOT EDIT."; \
     echo "// GENERATED FILE DO NOT EDIT"; \
     echo; \
     echo "package lexbase"; \
     echo; \
     grep '^const [A-Z][_A-Z0-9]* ' $(location //pkg/sql/parser:sql-goyacc)) > $@
    """,
    tools = [
        "//pkg/sql/parser:sql-goyacc",
    ],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

# Define the target to auto-generate the list of reserved keywords from the
# grammar file.
genrule(
    name = "gen-reserved-keywords",
    srcs = [
        "//pkg/sql/parser:sql.y",
        "//pkg/sql/parser:reserved_keywords.awk",
    ],
    outs = ["reserved_keywords.go"],
    cmd = """
          awk -f $(location //pkg/sql/parser:reserved_keywords.awk) < $(location //pkg/sql/parser:sql.y) > $@
    """,
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

exports_files(
    [
        "sql-gen.sh",
    ],
    visibility = ["//visibility:public"],
)
